export const toast = (message = '') => {
  setTimeout(() => {
    uni.showToast({
      title: message,
      icon: 'none',
      position: 'bottom',
      duration: 2000
    });
  }, 50)
}

export const success = (message = '') => {
  setTimeout(() => {
    uni.showToast({
      title: message,
      icon: 'success',
      position: 'center',
      duration: 3000
    });
  }, 50)
}

export const loading = (message = '') => {
  uni.showLoading({
    title: message,
    mask: true
  });
  uni.showNavigationBarLoading()
  return () => {
    uni.hideLoading()
    uni.hideNavigationBarLoading()
  }
}

export const modal = (opts = {}) => {
  return new Promise((resolve, reject) => {
    uni.showModal({
      ...opts,
      success(res) {
        return resolve(res.confirm)
      },
      fail(e) {
        return reject(e)
      }
    });
  })
}

export const alert = (title, content = '', confirmText = '确认') => {
  return modal({
    title,
    content,
    confirmText,
    showCancel: false
  })
}

export const confirm = (title, content = '', confirmText = '确认', cancelText = '取消') => {
  return modal({
    title,
    content,
    confirmText,
    cancelText
  })
}

export const actionSheet = (itemList = []) => {
  return new Promise((resolve, reject) => {
    uni.showActionSheet({
      itemList,
      success({
        tapIndex
      }) {
        resolve(tapIndex)
      },
      fail(e) {
        reject(e)
      }
    });
  })
}